import {
  AppVersionSchemaType,
  GetLatestVersionResponse,
  PostPublishAppProps,
  UpdateAppVersionBody,
  VersionListResponse,
} from '@/types/app/api';
import apiClient from '../apiClient';
import { PaginationProps } from '@/common';

export enum VersionApi {
  PublishApp = '/openapi/v2/apps/publish/',
  GetAppLatestVersion = '/api/ruigpt/main/application/version/latest',
  GetAppVersionDetail = '/api/ruigpt/main/application/version/detail',
  UpdateAppVersion = '/api/ruigpt/main/application/version/modify',
  GetWorkflowVersionList = '/api/ruigpt/main/application/version/list',
}

/**
 * 获取最新的版本
 * @param appId 应用id
 * @returns
 */
const getAppLatestVersion = (appId: string) =>
  apiClient.get<GetLatestVersionResponse>({
    url: VersionApi.GetAppLatestVersion,
    params: { appId },
  });

/**
 * 发布应用
 * @param appId 应用id
 * @param data 应用信息
 * @returns
 */
const postPublishApp = (appId: string, data: PostPublishAppProps) =>
  apiClient.post<VersionListResponse>({ url: `${VersionApi.PublishApp}/${appId}`, data });

export const getAppVersionDetail = (versionId: string, appId: string) =>
  apiClient.get<AppVersionSchemaType>({
    url: VersionApi.GetAppVersionDetail,
    params: { versionId, appId },
  });

export const updateAppVersion = (data: UpdateAppVersionBody) =>
  apiClient.post({ url: VersionApi.UpdateAppVersion, data });

export const getWorkflowVersionList = (data: PaginationProps<{ appId: string }>) =>
  apiClient.post<VersionListResponse>({ url: VersionApi.GetWorkflowVersionList, data });

export default {
  getAppLatestVersion,
  postPublishApp,
  updateAppVersion,
  getWorkflowVersionList,
};
